﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;

using DR.DBDomainModel;
using DR.Repository.Abstract;
using DR.WebUI.Code;
using DR.WebUI.Models.Enums;
using DR.WebUI.Models.Project;
using Omu.Awesome.Core;


namespace DR.WebUI.Controllers
{
    [Authorize(Roles = "manager, topmanager")]
    public class ProjectController : Controller
    {
        private readonly IProjectRepository projectRepository;
        private readonly IDeveloperProjectRepository developerProjectRepository;

        public ProjectController(IProjectRepository projectRepository, IDeveloperProjectRepository developerProjectRepository)
        {
            this.projectRepository = projectRepository;
            this.developerProjectRepository = developerProjectRepository;
            PageSize = 15;
        }

        public int PageSize { get; set; }

        public ViewResult List(ProjectFilter filter, int page = 1)
        {
            IList<ViewProject> projects = projectRepository
                .SelectByFilter(filter.ProjectName, filter.DateLeft, filter.DateRight, filter.SelectedProjectStatuses)    
                .Select(r => new ViewProject
                                    {
                                        ID = r.ProjectID,
                                        Title = r.Name,
                                        Status = (ProjectStatus) r.Status,
                                        DateCreate = r.DateCreate,
                                        Description = r.Description,
                                        FinishDate = r.DateFinish,
                                        StartDate = r.DateStart
                                    }).ToList();

            ViewData["ProjectFilter"] = filter;
            ViewData["ProjectStatuses"] = MiscHelpers.GetAllProjectStatuses();

            return View(new Pageable<ViewProject>
            {
                PageIndex = page,
                PageCount = MiscHelpers.GetPageCount(PageSize, projects.Count),
                Page = projects.Skip((page - 1) * PageSize).Take(PageSize)
            });
        }

        #region Filter Project
        [HttpPost]
        public ActionResult SetupProjectFilter(ProjectFilter filter, string filterButton, int[] statuses, string projectName, DateTime? dateLeft, DateTime? dateRight)
        {
            if (filterButton == "Apply")
            {
                filter.ProjectName = projectName;
                filter.DateLeft = dateLeft;
                filter.DateRight = dateRight;
                filter.SelectedProjectStatuses.Clear();
                if (statuses != null)
                {
                    filter.SelectedProjectStatuses.UnionWith(statuses);
                }
            }
            else
            {
                filter.ProjectName = projectName;
                filter.DateLeft = dateLeft;
                filter.DateRight = dateRight;
                filter.SelectedProjectStatuses.Clear();
                filter.SelectedProjectStatuses.UnionWith(Enum.GetValues(typeof(ProjectStatus)).Cast<int>());
            }
            return RedirectToAction("List");
        }
        #endregion //Filter Project

        #region Add New Project
        [HttpGet]
        [Authorize(Roles = "topmanager")]
        public ActionResult Create()
        {
            ViewData["ProjectStatuses"] = MiscHelpers.GetAllProjectStatuses().Select(r => new SelectListItem
            {
                Value = r.StatusID.ToString(),
                Text = r.Text
            }).ToList();

            var newProject = new NewProject { StartDate = DateTime.Now };

            return View(newProject);
        }

        [HttpPost]
        [Authorize(Roles = "topmanager")]
        public ActionResult Create(NewProject newProject)
        {
            projectRepository.Insert(new Project
            {
                DateCreate = DateTime.Now,
                DateStart = newProject.StartDate,
                DateFinish = newProject.FinishDate,

                Description = newProject.Description,
                Name = newProject.ProjectName,
                Status = (int)newProject.Status
            });

            return RedirectToAction("List");
        }

        #endregion //Add New Project

        #region Edit Project
        [HttpGet]
        public ActionResult Edit(int projectID)
        {
            var project = projectRepository.SelectByID(projectID);
            var editProject = new EditProject
            {
                ProjectID = project.ProjectID,
                ProjectName = project.Name,
                DateCreate = project.DateCreate,
                DateStart = project.DateStart,
                DateFinish = project.DateFinish,
                Description = project.Description,
                Status = (ProjectStatus)project.Status
            };

            ViewData["ProjectStatuses"] = MiscHelpers.GetAllProjectStatuses().Select(r => new SelectListItem
            {
                Value = r.StatusID.ToString(),
                Text = r.Text,
                Selected = (project.Status == r.StatusID)
            }).ToList();

            return View(editProject);
        }

        
        [HttpPost]
        [Authorize(Roles = "TopManager")]
        public ActionResult Edit(EditProject editedProject)
        {
            if (ModelState.IsValid)
            {
                var project = new Project
                {
                    ProjectID = editedProject.ProjectID,
                    Name = editedProject.ProjectName,
                    DateCreate = editedProject.DateCreate,
                    DateStart = editedProject.DateStart,
                    DateFinish = editedProject.DateFinish,
                    Description = editedProject.Description,
                    Status = (int)editedProject.Status
                };
                projectRepository.Update(project);
            }

            
            return RedirectToAction("List");
        }
        #endregion //Edit Project

      
        [HttpGet]
        public ActionResult ViewProjectDetails(int projectid)
        {
           var project = projectRepository.SelectByID(projectid);
           var projectDetail = new ProjectDetailModel()
            {
                ProjectID = project.ProjectID,
                ProjectName = project.Name,
                DateStart = FormatHelpers.FormatDate(project.DateStart),
                DateCreate = FormatHelpers.FormatDate(project.DateCreate),
                FinishDate = FormatHelpers.FormatDate(project.DateFinish),
                State = project.Status,
                Description = project.Description
            };

            return View(projectDetail);
        }

        [HttpPost]
        [Authorize(Roles = "topmanager")]
        public JsonResult Delete(int projectID)
        {
            projectRepository.Delete(projectID);

            return Json(new { result = true });
        }

        [HttpPost]
        [Authorize(Roles = "topmanager")]
        public JsonResult IsDeleted(int projectID)
        {
            DeveloperProject singleOrDefault = developerProjectRepository.SelectAll().FirstOrDefault(n => n.ProjectID == projectID);

            return Json(singleOrDefault == null ? new { result = false } : new { result = true });
        }
    }
}










